//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    list: [{
        code: 'menu',
        label: '菜单栏'
      },
      {
        code: 'recorder',
        label: '语音'
      },
      {
        code: 'alert',
        label: '提示',
        children: [{
            code: 'alert-success',
            label: '成功提示'
          },
          {
            code: 'alert-error',
            label: '失败提示'
          },
          {
            code: 'alert-loading',
            label: 'loading'
          },
        ]
      },
      {
        code: 'list',
        label: '列表下拉',
        jump: '/pages/example/list/index'
      },
      {
        code: 'imgClip',
        label: '图片裁剪',
        jump: '/pages/example/imgClip/index'
      },
    ],
    choose: null,
    alert: {
      type: null,
      text: '',
      show: false
    },
    st: null
  },
  onShow(){
    if (this.data.choose) {
      setTimeout(() => {
        this.setData({
          choose: null
        })
      }, 200)
    }
  },
  clickBtn(e) {
    let item = e.target.dataset.item
    let code=item.code
    this.setData({
      choose: code === this.data.choose ? null : code
    })
    if (item.jump) {
      wx.redirectTo({ url: item.jump, })
      // wx.navigateTo({ url: item.jump, })
      return
    }
    switch (code) {
      case 'alert-success':
        this.setData({
          'alert.type': 'success',
          'alert.text': '成功提示',
          'alert.show': true,
        })
        break;
      case 'alert-error':
        this.setData({
          'alert.type': 'error',
          'alert.text': '失败提示',
          'alert.show': true,
        })
        break;
      case 'alert-loading':
        this.setData({
          'alert.type': 'loading',
          'alert.text': '加载中',
          'alert.show': true,
        })
        setTimeout(() => {
          this.setData({
            choose: null,
            'alert.show': false,
          })
        }, 5000)
        return;
    }
    if (code.indexOf('alert-') > -1) {
      clearTimeout(this.data.st)
      let st = setTimeout(() => {
        if (this.data.alert.type =='loading'){
          return
        }
        this.setData({
          choose: null,
        })
      }, 2020)
      this.setData({
        st
      })
    }
  },
  recordFun(res) {
    console.log('录音文件：', res)
  },
  menuFun(res) {
    console.log('菜单刷新')
  }
})